<?php
 class Lineproduct extends BZ_Controller {
         
     public function _init() {
         
         $this->load->helper(array('common'));
         $this->load->model('line_img_model', 'line_img');
         $this->load->model('line_base_model','line_base');
         $this->load->model('line_route_model', 'line_route');
         $this->load->model('line_price_model','line_price');
         $this->load->model('line_product_reviews_model', 'line_product_reviews');
     }
     
     public function view($line_id=0) {
        
         if ( !empty($line_id) && !is_numeric($line_id) ) {
             $this->alertJumpPre('此产品不存在。');
         }
         $data['line_img'] = $this->line_img->findByLineId($line_id); 
         $result = $this->line_base->findViewById($line_id);
         if ( $result->num_rows() <= 0 ) {
             $this->alertJumpPre('此产品不存在，或者已下架。');
         }
         $data['line_base']        = $result->row();
         $data['line_route']       = $this->line_route->findByLineId($line_id);
         $data['line_comment_total'] = $this->line_product_reviews->getProductReviewsCount($line_id);
         $data['line_comment'] = $this->line_product_reviews->getProductReviewsPage($pageNum=10,$num=0,$line_id);
         $data['line_comment_page'] = ceil($data['line_comment_total']/10);
         $data['name'] = $result->row()->line_name;
         $data['back_url'] = 'javascript:history.back()';
         $data['use_base_url'] = 'on';
         $this->load->view('line/lineproduct/productview',$data);
     }
     
     public function viewDate($line_id) {
     	
         $line_price = $this->line_price->findByLineIdDate($line_id);
         $data['line_price_json']  = $this->getPriceJson($line_price);
         $data['use_base_url'] = 'on';
         $data['back_url'] = 'javascript:history.back()';
         $data['name'] = '出行日期';
         $data['line_id'] = $line_id;
         $this->load->view('line/lineproduct/productdate',$data);
     }
     
     public function getPriceJson($line_price) {
         
         $priceJson = array();
         if ($line_price->num_rows() > 0) {
             $i = 0;
             foreach ($line_price->result() as $key=>$item) {
                 //如果日历-提前预定时间，大于当前时间
                 if (strtotime(date('Y-m-d', strtotime($item->start_date)).' 23:59:59')-$item->open_time*60*60 < time()) {
                     continue;
                 }
                 $priceJson[$i]['price_id']  = $item->id;
                 $priceJson[$i]['DateWorth'] = date('Y-m-d',strtotime($item->start_date));
                 $priceJson[$i]['Price']     = $item->adult_original_price;
                 $priceJson[$i]['Child']     = $item->children_original_price;
                 $priceJson[$i]['Room']      = $item->line_stock;
                 $i++;
             }
         }
         return json_encode($priceJson);
     }
     
     public function ajaxGetLineProductView(){
         
         $pg = $this->input->get('page') ? $this->input->get('page') : '2';
         $pageNum = 10;
         $num = ($pg-1 )*$pageNum;
         $line_id = trim($this->input->get('line_id'));
         $data['line_comment'] = $this->line_product_reviews->getProductReviewsPage($pageNum=10,$num,$line_id);
         if($data['line_comment']->num_rows() > 0){
         	echo json_encode(array('status'=>true,'content'=>trim($this->load->view('line/lineproduct/productcomment',$data,true))));exit;
         }else{
         	echo json_encode(array('status'=>false,'content'=>'已无更多'));exit;
         }
     }
     
     public function ajaxGetLineStock(){
       
         $price_id = $this->input->get('price_id');
         $line_price = $this->line_price->findById($price_id);
         if( $line_price->num_rows() > 0 ){
              $data['start_date'] = date('Y-m-d',strtotime($line_price->row()->start_date));
              $data['line_stock'] = $line_price->row()->line_stock;
         }
         echo json_encode(array('status'=>true,'content'=>$this->load->view('line/lineproduct/productlinestock',$data,true)));exit;
     }
 }